<template>
  <tl-sub-page :append-to-body="true" :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1000px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="canDisable">
      <el-row>
        <el-col :span="24">
          <el-form-item label="所属里程碑及任务" prop="milepostname" :rules="rules.validateRequire">
            <el-input v-model="model.milepostname" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openMpmProjectMilepostDialog" v-if="!disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="12">
          <el-form-item label="任务类型" prop="tasktype" :rules="rules.validateRequire">
            <el-select v-model="model.tasktype" filterable clearable>
              <el-option v-for="item in codeModel.tasktypeList" :key="item.code" :label="item.name"
                :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="优先级" prop="taskpriority" :rules="rules.validateRequire">
            <el-select v-model="model.taskpriority" filterable clearable>
              <el-option v-for="item in codeModel.taskpriorityList" :key="item.code" :label="item.name"
                :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="任务名称" prop="taskname" :rules="rules.validateRequire">
            <el-input v-model="model.taskname"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="任务内容" prop="taskdetail" :rules="rules.validateRequire">
            <el-input v-model="model.taskdetail" type="textarea" maxlength="1000" show-word-limit :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="责任人" prop="responsible" :rules="rules.validateRequire">
            <el-input v-model="model.responsible" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openMpmProjectTeamDialog" v-if="!canDisable"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="责任人工号" prop="responsiblecode" :rules="rules.validateRequire">
            <el-input v-model="model.responsiblecode" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划开始日期" prop="planstartdate" :rules="rules.validateRequire">
            <el-date-picker v-model="model.planstartdate" type="date" :disabled="canChange" placeholder="例：2021-01-01" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划完成日期" prop="planenddate" :rules="rules.validateRequire">
            <el-date-picker v-model="model.planenddate" type="date" placeholder="例：2021-01-01" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="12">
          <el-form-item label="任务进度" prop="taskschedule">
            <el-input-number v-model="model.taskschedule" :disabled="true" style="width:95%" controls-position="right">
            </el-input-number><span>%</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务状态" prop="taskstate">
            <el-select v-model="model.taskstate" filterable clearable :disabled="true">
              <el-option v-for="item in codeModel.taskstateList" :key="item.code" :label="item.name"
                :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row> -->
      <!-- <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row> -->
    </el-form>
    <tl-approval-history ref="history" sourceType="MpmProjectTask" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <!-- <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!canDisable">
        <svg-icon icon-class="save" />保存
      </el-button>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />下发任务
      </el-button> -->
      <!-- <tl-attachment sourceType="MpmProjectTask" :sourceId="model.id" :disabled="disableAttachment"
        :editable="stateEdit"></tl-attachment> -->
      <!-- <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button> -->
      <!-- <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button> -->
    </div>
    <tlmpmProject-milepost-ref ref="mpmProjectMilepostRef" @confirm="getMpmProjectMilepostData" :isSingleSelect="true" :externalQueryParam="externalQueryParamForPool">
    </tlmpmProject-milepost-ref>
    <tlmpmProject-team-ref ref="mpmProjectTeamRef" @confirm="getmpmProjectTeamData" :isSingleSelect="true" :externalQueryParam="externalQueryParamForTeam"></tlmpmProject-team-ref>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlmpmProjectMilepostRef from '@/views/mpm/mpmProjectMilepost/refList'
import tlmpmProjectTeamRef from '@/views/mpm/mpmProjectTeam/refList'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlmpmProjectMilepostRef, tlmpmProjectTeamRef },
  data() {
    return {
      moduleTitle: '项目任务',
      urlPrefix: '/mpm/projectTask/',
      canDisable: true,
      canChange: false,
      url: {
        // 额外的url
      },
      externalQueryParamForPool: {},
      externalQueryParamForTeam: {}
    }
  },
  props: {
    projectinfo: {}
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        this.$set(this.model, 'applyorgcode', this.org.code)
        this.$set(this.model, 'applyorgname', this.org.name)
        this.$set(this.model, 'applydeptcode', this.dept.code)
        this.$set(this.model, 'applydeptname', this.dept.fullname)
        this.$set(this.model, 'applyusercode', this.user.code)
        this.$set(this.model, 'applyusername', this.user.name)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'deptcode', this.dept.code)
        this.$set(this.model, 'orgcode', this.org.code)
        this.$set(this.model, 'taskschedule', '0')
        this.$set(this.model, 'taskstate', '0')
        this.$set(this.model, 'projectcode', this.projectinfo.projectcode)
        this.$set(this.model, 'projectname', this.projectinfo.projectname)
      }
    },
    loaded() {
      // 页面加载完毕执行
      //   if (!this.isAdd && this.model.docno) {
      //     this.title = this.title + `（单据编号：${this.model.docno}）`
      //   }
      this.model.taskstate = String(this.model.taskstate)
      this.canDisable = true
      this.canChange = false
      let code = this.projectinfo.projectmanagercode
      // 项目经理
      if (code === this.user.code && this.model.taskstate === '0') {
        this.canDisable = false
      }
      // 经营管理部
      if (this.$store.getters.projectroles.indexOf('pmo') > -1 && this.model.taskstate === '0') {
        this.canDisable = false
      }
      // 经营管理部
      if (this.$store.getters.projectroles.indexOf('pmo') > -1 && this.model.taskstate === '1') {
        this.canDisable = false
        this.canChange = true
      }
      if (this.model.state === 0 || this.model.state === 2) {
        this.canDisable = false
      }
    },
    /** 提交审批 */
    doSubmitApproval() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (!this.url.submit) {
            this.$tap.alert('请设置审批地址!', 'warning')
            return
          }
          if (!this.beforeApproval()) {
            return
          }
          this.submitLoading = true
              putAction(this.url.submit, { modelArray: this.model }).then(res => {
                if (res.success) {
                  this.$tap.message(res.message, 'success')
                  this.queryData(this.model.id)
                  this.afterApproval()
                } else {
                  this.$tap.alert(res.message, 'warning')
                }
                this.submitLoading = false
              })
        }
      })
    },
    /** 打开里程碑及任务参照 */
    openMpmProjectMilepostDialog() {
      const { projectcode } = this.model
      this.externalQueryParamForPool = {
        qry_projectcode: projectcode,
        qry_enabled: 1,
        qry_milepoststate: 0
      }
      this.$refs.mpmProjectMilepostRef.visible = true
    },
    openMpmProjectTeamDialog() {
      const { projectcode } = this.model
      this.externalQueryParamForTeam = {
        qry_projectcode: projectcode,
        qry_state: 9,
        qry_appointmenttype: 1,
        qry_enabled: 1
      }
      this.$refs.mpmProjectTeamRef.visible = true
    },
    /** 获取里程碑及任务参照数据 */
    getMpmProjectMilepostData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'milepostname', selectionRow.milepostname)
      this.$set(this.model, 'milepostcode', selectionRow.operatecode)
    },
    getmpmProjectTeamData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'responsiblecode', selectionRow.usercode)
      this.$set(this.model, 'responsible', selectionRow.username)
    },
    /** 页面打开时执行 - zhanglongfei 2019.10.18 */
    open() {
      this.model = {}
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '新增' + this.moduleTitle
          // 需要添加重复判断
          if (this.$store.getters.param.isAvoidRepeat === true) {
            this.queryBillUid()
          }
          if (this.isQueryInitData === true) {
            // 后台进行数据初始化
            this.queryInitData().then(_ => {
              this.assignDefaultValue()
              this.loaded()
            })
          } else {
            // 前台进行数据初始化
            this.$set(this.model, 'enabled', 1)
            this.$set(this.model, 'sortcode', 0)
            this.assignDefaultValue()
            this.loaded()
          }
        } else if (this.disableEditSubmit === false) {
          this.title = '编辑' + this.moduleTitle
          this.queryData(this.id).then(_ => {
            this.loaded()
          })
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryData(this.id).then(_ => {
            this.loaded()
          })
        }
      })
    }
  }
}
</script>

